package com.dashu.core.service;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.core.result.PageEnty;
import com.dashu.base.utils.CopyUtil;
import com.dashu.core.dto.OperatorDTO;
import com.dashu.core.handler.VerificationException;
import com.dashu.core.model.Operator;
import com.dashu.core.model.QOperator;
import com.dashu.core.vo.OperatorVO;
import com.dashu.core.vo.UpdateEntityVO;
import com.querydsl.core.BooleanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.persistence.FlushModeType;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OperatorService extends DataService {

    @Transactional
    public OperatorDTO add(OperatorVO operatorVO) {
        Operator operator = CopyUtil.copyEnty(operatorVO, Operator.class);
        try {
            entityManager.persist(operator);
//            entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new VerificationException(ReplRyCode.DATA_NOT_INVALID, ReplRyCode.DATA_NOT_INVALID.getMessage());
        }
        OperatorDTO operatorDTO = CopyUtil.copyEnty(operator, OperatorDTO.class);
        return operatorDTO;

    }

    @Transactional
    public boolean deleteByOperatorId(long OperatorId) {
        QOperator qOperator = QOperator.operator;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qOperator.id.eq(OperatorId));
        Operator operator;

            queryFactory.select();
            operator = queryFactory
                    .select(qOperator)
                    .from(qOperator)
                    .where(booleanBuilder.getValue()).fetchOne();
            if (operator == null) return false;
            entityManager.remove(operator);
//            entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);
        return true;

    }

    @Transactional
    public OperatorDTO updataByOperatorId(UpdateEntityVO<OperatorVO> updateOperatorVO) {
        QOperator qOperator = QOperator.operator;
        OperatorVO operatorVO = updateOperatorVO.getEntityVO();
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qOperator.id.eq(updateOperatorVO.getId()));
        Operator operator;
        try {
            operator = queryFactory
                    .select(qOperator)
                    .from(qOperator)
                    .where(booleanBuilder.getValue()).fetchOne();
            if (operator == null) return null;
            operator.setOperatorsName(operatorVO.getOperatorsName());
            operator.setChargeName(operatorVO.getChargeName());
            operator.setGender(operatorVO.getGender());
            operator.setChargePhoner(operatorVO.getChargePhoner());
            operator.setAddress(operatorVO.getAddress());
            operator.setUpdateTime(new Date().getTime());
            operator = entityManager.merge(operator);
            operatorVO.getSchools().stream().forEach(s -> {

            });
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
//        entityManager.flush();
        entityManager.setFlushMode(FlushModeType.AUTO);
        OperatorDTO operatorDTO = CopyUtil.copyEnty(operator, OperatorDTO.class);
        return operatorDTO;

    }

    public List<OperatorDTO> findByCreteUser( PageEnty<Long> createUserVo) {
        QOperator qOperator = QOperator.operator;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qOperator.createUser.eq(createUserVo.getBaseId()));
        List<Operator> operators = queryFactory
                .select(qOperator)
                .from(qOperator)
                .where(booleanBuilder.getValue())
                .fetch();
        List<OperatorDTO> operatorDTOS = CopyUtil.copyList(operators, OperatorDTO.class);
        return operatorDTOS;

    }

    public OperatorDTO findByOperatorId(long OperatorId) {
        QOperator qOperator = QOperator.operator;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qOperator.id.eq( OperatorId));
        Operator operator = queryFactory
                .select(qOperator)
                .from(qOperator)
                .where(booleanBuilder.getValue()).fetchOne();
        if (operator == null) return null;
        OperatorDTO operatorDTO = CopyUtil.copyEnty(operator, OperatorDTO.class);
        return operatorDTO;

    }

}